#!/usr/bin/python3
# -*- coding: utf-8 -*-

#  install
# "pip install npyscreen" OR "pip install -r requirements.txt"

# 连接SimData 首先向SimData目标机发送 [终端名称]-[UDP端口] 如("terminal1-8888") 的字符串,目标及返回"connect"表示连接成功，返回"erro"表示连接失败

import sys
import npyscreen
import socket

name = 'TestTerminal'
port = '1990'
state = 'STOP'

def Terminal(name, port):
    msg = name + "-" + port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    host = socket.gethostname() 
    s.connect((host, 1989))
    s.send(msg.encode(encoding="utf-8"))


class MyTestApp(npyscreen.NPSAppManaged):
    # socket code
    def onStart(self):
        self.keypress_timeout_default = 1
        #npyscreen.setTheme(npyscreen.Themes.BlackOnWhiteTheme)
        self.addForm("MAIN",       MainForm, name="SIM DATA TEST", color="IMPORTANT")
        
        
        self.addr = ('', int(port))
        self.udpServer = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        self.udpServer.bind(self.addr)

        self.terminal = name
        self.port = port
        self.state="START "
        self.currentform = None
        self.Data = []


    def while_waiting(self):
        data,self.addr = self.udpServer.recvfrom(1024)
        data  = data.decode(encoding='utf-8').upper()
        self.Data = [data]
             
             

    def onCleanExit(self):
        npyscreen.notify_wait("Bye Bye!")
        
        self.udpServer.close()
    
    def change_form(self, name):
        self.switchForm(name)
        self.resetHistory()
    
class MainForm(npyscreen.ActionForm):
    def create(self):
        self.keypress_timeout_default = 1
        self.TerminalField = self.add(npyscreen.TitleText, name = "Terminal:", value= "" )
        self.PortField = self.add(npyscreen.TitleText, name = "Port:", value="", editable=False )
        self.STATEfield = self.add(npyscreen.TitleText, name = "STATE:", value="", editable=False )
        self.data1Field = self.add(npyscreen.BoxTitle, name="Data1:",rely=5 ,relx=1, max_width=20 )
        self.data2Field = self.add(npyscreen.BoxTitle, name="Data2:",rely=5 ,relx=21, max_width=20 )
        self.data3Field = self.add(npyscreen.BoxTitle, name="Data3:",rely=5 ,relx=41, max_width=20 )
        self.data4Field = self.add(npyscreen.BoxTitle, name="Data4:",rely=5 ,relx=61, max_width=20 )
        self.data5Field = self.add(npyscreen.BoxTitle, name="Data5:",rely=5 ,relx=81, max_width=20 )

    def while_waiting(self):
        self.TerminalField.value = self.parentApp.terminal
        self.PortField.value = self.parentApp.port
        self.STATEfield.value = self.parentApp.state

        self.data1Field.values = self.parentApp.Data

        self.TerminalField.display()
        self.PortField.display()
        self.STATEfield.display()

        self.data1Field.display()





    def on_ok(self):
        # Exit the application if the OK button is pressed.
        self.parentApp.switchForm(None)

def main():
    TA = MyTestApp()
    TA.run()


if __name__ == '__main__':
    name = input("Terminal：")
    port = input("Port:")
    Terminal(name, port)
    main()

      